﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using ICSharpCode.SharpZipLib.GZip;


public class GZipDecompresser : Singleton<GZipDecompresser>
{
    /// <summary>
    /// ZIP解压
    /// </summary>
    /// <param name="zippedData"></param>
    /// <returns></returns>
    public static byte[] Decompress(byte[] zippedData)
    {
        //UnityEngine.Debug.Log(Binutils.HexDump(zippedData));

        MemoryStream ms = new MemoryStream(zippedData);
        GZipInputStream compressedzipStream = new GZipInputStream(ms, zippedData.Length);

        MemoryStream outBuffer = new MemoryStream();
        byte[] block = new byte[1024];
        while (true)
        {
            int bytesRead = compressedzipStream.Read(block, 0, block.Length);
            if (bytesRead <= 0)
                break;
            else
                outBuffer.Write(block, 0, bytesRead);
        }
        compressedzipStream.Close();
        return outBuffer.ToArray();
    }

    public static ThreadJob DecompressAsync(byte[] zippedData)
    {
        return AppFacade.threadManager.SetJob(() =>
        {
            return Decompress(zippedData);
        });
    }
}
